<?php 
/*
 module:		题目详情
 create_time:	2021-09-03 00:14:09
 author:		
 contact:		
*/

namespace app\admin\controller\Topic;

use app\admin\service\Topic\DetailsService;
use app\admin\model\Topic\Details as DetailsModel;
use app\admin\controller\Admin;
use think\facade\Db;

class Details extends Admin {


	/*首页数据列表*/
	function index(){
		if (!$this->request->isAjax()){
			return view('index');
		}else{
			$limit  = $this->request->post('limit', 20, 'intval');
			$offset = $this->request->post('offset', 0, 'intval');
			$page   = floor($offset / $limit) +1 ;

			$where = [];
			$where['topic_id'] = $this->request->param('topic_id', '', 'serach_in');
            $where['type'] = $this->request->param('type', '', 'serach_in');
			$order  = $this->request->post('order', '', 'serach_in');	//排序字段 bootstrap-table 传入
			$sort  = $this->request->post('sort', '', 'serach_in');		//排序方式 desc 或 asc

			$field = 'details_id,title,img,options,fraction,type';
			$orderby = ($sort && $order) ? $sort.' '.$order : 'details_id desc';

			$res = DetailsService::indexList(formatWhere($where),$field,$orderby,$limit,$page);

			return json($res);
		}
	}

	/*添加*/
	function add(){
		if (!$this->request->isPost()){
			return view('add');
		}else{
            $postField = 'title,img,options,topic_id,fraction,type';
            $data = $this->request->only(explode(',',$postField),'post',null);
            $data['options'] = implode($data['options'],',_');
            $data['fraction'] = implode($data['fraction'],',_');
			$res = DetailsService::add($data);
			return json(['status'=>'00','msg'=>'添加成功']);
		}
	}

	/*修改*/
	function edit(){
		if (!$this->request->isPost()){
			$details_id = $this->request->get('details_id','','serach_in');
			if(!$details_id) $this->error('参数错误');
			$info = checkData(DetailsModel::find($details_id));
			$list = [];
            $info['options'] = explode(',_',$info['options']);
            $info['fraction'] = explode(',_',$info['fraction']);
            foreach ($info['fraction'] as $k=>$v){
                $arr = [];
                $arr['options'] = $info['options'][$k];
                $arr['fraction'] = $v;
                $list[]=$arr;
            }
            $this->view->assign('info',$info);
            $this->view->assign('list',$list);
			return view('edit');
		}else{
			$postField = 'details_id,title,img,options,topic_id,fraction,type';
			$data = $this->request->only(explode(',',$postField),'post',null);
            $data['options'] = implode($data['options'],',_');
            $data['fraction'] = implode($data['fraction'],',_');
			$res = DetailsService::edit($data);
			return json(['status'=>'00','msg'=>'修改成功']);
		}
	}

	/*删除*/
	function del(){
		$idx =  $this->request->post('details_id', '', 'serach_in');
		if(!$idx) $this->error('参数错误');
		try{
			DetailsModel::destroy(['details_id'=>explode(',',$idx)],true);
		}catch(\Exception $e){
			abort(config('my.error_log_code'),$e->getMessage());
		}
		return json(['status'=>'00','msg'=>'操作成功']);
	}



}

